package com.zhanggm.student.controller;


import com.github.pagehelper.PageInfo;
import com.zhanggm.student.config.ResponseResult;
import com.zhanggm.student.entity.Student;
import com.zhanggm.student.entity.StudentHobbyRelation;
import com.zhanggm.student.model.StudentModel;
import com.zhanggm.student.service.IStudentHobbyRelationService;
import com.zhanggm.student.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-10-21
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IStudentHobbyRelationService iStudentHobbyRelationService;

    @RequestMapping("/getPageInfo")
    public ResponseResult getPageInfo(StudentModel studentModel,
                                      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){

        PageInfo<StudentModel> pageInfo = iStudentService.getPageInfo(studentModel, pageNum, pageSize);
        log.info("list:{}",pageInfo.getList());
        return ResponseResult.success(pageInfo);
    }

    @PostMapping("batchDel")
    public ResponseResult batchDel(@RequestBody StudentModel studentModel){
        List<Integer> stuIdList = studentModel.getStuIdList();
        log.info("stuIdList:{}",stuIdList);
        boolean b = iStudentService.removeByIds(stuIdList);
        return ResponseResult.success(b);
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody StudentModel studentModel){
//        studentModel.setJoinTime(LocalDateTime.now());
        //保存学生信息
        boolean result = iStudentService.saveOrUpdate(studentModel);
        //根据学生Id删除关系表数据
        Map<String,Object> delWhereMap = new HashMap<>();
        delWhereMap.put("stu_id",studentModel.getId());
        iStudentHobbyRelationService.removeByMap(delWhereMap);
        //保存爱好数据
        if(studentModel.getHobbyIdList()!=null){
            studentModel.getHobbyIdList().forEach(hobbyId->{
                StudentHobbyRelation studentHobbyRelation = new StudentHobbyRelation();
                studentHobbyRelation.setStuId(studentModel.getId());
                studentHobbyRelation.setHobbyId(hobbyId);
                iStudentHobbyRelationService.save(studentHobbyRelation);
            });
        }
        log.info("保存成功:{}",studentModel.getId());
        return ResponseResult.success(result);
    }

    @GetMapping("getById")
    public ResponseResult getById(Integer id){
        StudentModel studentModel = new StudentModel();
        //查询学生基本信息
        Student student = iStudentService.getById(id);
        BeanUtils.copyProperties(student,studentModel);
        //查询爱好Id列表
        /*QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stu_id",student.getId());
        List<StudentHobbyRelation> studentHobbyRelationList = iStudentHobbyRelationService.list(queryWrapper);
        //使用stream流获取hobbyIdList
        List<Integer> hobbyIdList = studentHobbyRelationList.stream().map(c->c.getHobbyId()).collect(Collectors.toList());*/
        List<Integer> hobbyIdList = iStudentService.getHobbyIdListByStuId(student.getId());
        //设置爱好IdList
        studentModel.setHobbyIdList(hobbyIdList);
        return ResponseResult.success(studentModel);
    }

}
